package com.ktjy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 许亚轩
 * @version 1.0
 * @date 2024/11/13 8:39
 */
public class MyBatisUtil {
    static SqlSessionFactory sessionFactory = null;
    static {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        sessionFactory = builder.build(resourceAsStream);
    }
    public static SqlSession createSqlSession() {
        return sessionFactory.openSession(false);
    }
    public static void closeSqlSession(SqlSession session) {
        if (session!=null){
            session.close();
        }
    }
}
